<?php
/**
 *
 * @package 
 * @since 1.0
 * @todo 登陆 注册
 */
 
class UserAction extends AllAction{
	
	function check_user_name(){
		$user_name=$_POST['user_name'];
		if(M('user')->where("user_name='$user_name'")->find()){
			echo "1";
		}else{
			echo "0";
		}
		exit;
	}
	function check_nicheng(){
		$nicheng=$_POST['nicheng'];
		if(M('user')->where("nicheng='$nicheng'")->find()){
			echo "1";
		}else{
			echo "0";
		}
		exit;
	}
	function check_vcode(){
		$vcode=$_POST['vcode'];
		if(strtolower($vcode)!=strtolower($_SESSION['vcode'])){
			echo "1";
		}else{
			echo "0";
		}
		exit;
	}

	function reg(){
		
		$this->display();	
		
	}
	
	function reg_do(){
		$yanzheng=strtolower($_POST["vcode"]);
		$aa=strtolower($_SESSION['vcode']);
		if($aa!=$yanzheng){
			$this->error("验证码错误");	exit;
		}
		if($_POST['user_name']=='' || $_POST["user_pass"]=='' || $_POST["nicheng"]==''){
			$this->error("不能为空");	exit;
		}
		if(M('user')->where("user_name='".$_POST['user_name']."'")->find()){
			$this->error("此邮箱已经注册");exit;
		}
		if(M('user')->where("nicheng='".$_POST['nicheng']."'")->find()){
			$this->error("此昵称已有人使用");exit;
		}
		$_POST['ctime']=time();
		$_POST['user_pass']=md5($_POST['user_pass']);
		$_POST['yueducishu']="10";
		$_POST['zancishu']="10";
		$_POST['caicishu']="10";
		$_POST['zhuanfacishu']="10";
		$_POST['pingluncishu']="10";
		$_POST['zhidingcishu']="1";
		//邮箱验证激活
		$_POST['jihuoma'] = md5($_POST['user_name'].$_POST['ctime']);
		$_POST['jihuoma_exptime'] = $_POST['ctime']+60*60*24;
		
		if($user_id=M("user")->add($_POST)){
			$haoyou['bei_id']=$user_id;
			$haoyou['zhu_id']=$user_id;
			$yiyouhaoyou=M('haoyou')->where("zhu_id=".$haoyou['zhu_id']." and bei_id=".$haoyou['bei_id']."")->select();
			if($yiyouhaoyou==""){
				M('haoyou')->add($haoyou);
			}

		//发送验证邮件
//		$smtpserver = "smtp.qq.com"; //SMTP服务器，如：smtp.163.com 
//		$smtpserverport = 25; //SMTP服务器端口，一般为25 
//		$smtpusermail = "568601818@qq.com"; //SMTP服务器的用户邮箱，如xxx@163.com 
//		$smtpuser = "568601818"; //SMTP服务器的用户帐号xxx@163.com 
//		$smtppass = "guoernai1233"; //SMTP服务器的用户密码 
//		$smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //实例化邮件类 
//		$emailtype = "HTML"; //信件类型，文本:text；网页：HTML 
//		$smtpemailto = $_POST['user_name']; //接收邮件方，本例为注册用户的Email 
//		$smtpemailfrom = $smtpusermail; //发送邮件方，如xxx@163.com 
//		$emailsubject = "乐嘻游用户帐号激活";//邮件标题 
//		//邮件主体内容 
//		$emailbody = "亲爱的".$_POST['nicheng']."：<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/> 
//		<a href='http://yingshi.bjdingzhicheng.com/index.php?a=active&m=user&verify=".$_POST['token']."' target= 
//	'_blank'>http://yingshi.bjdingzhicheng.com/index.php?a=active&m=user&verify=".$_POST['token']."</a><br/> 
//		如果以上链接无法点击，请将它复制到你的浏览器地址栏中进入访问，该链接24小时内有效。"; 
//		//发送邮件
//		$smtp->debug = false; 
//		$smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); 
		$ver=$_POST['jihuoma'];
		$ll="http://yingshi.bjdingzhicheng.com/index.php?a=jihuo&m=User&ket=".$ver."&id=".$user_id;
		sendemail($_POST['user_name'], 'lexiyou', $_POST['user_name'], "您好，".$_POST['nicheng']."  复制下面地址,到浏览器中打开\r\n".$ll."\r\n"."此邮件是验证邮件，不必回复。","会员注册验证");
		
		$_SESSION["user_id"]=$user_id;
		$_SESSION["user_name"]=$_POST["user_name"];
		$_SESSION["nicheng"]=$_POST["nicheng"];
		$this->assign('jumpUrl', U('Index/grsz'));
		$this->success("恭喜您，注册成功！请登录到您的邮箱及时激活您的帐号！");
		}else{
		$this->assign('jumpUrl', U('Index/index'));
		$this->error("注册失败！");
		}
		$this->display('Index/grsz');
	}
	
	function jihuo(){
		$verify = $_GET['ket'];
		$id=$_GET['id']; 
		 
		$row = M('user')->where("jihuoma='$verify' and id='$id'")->find(); 
		if($row){ 
			$row['status']="1"; 
			$row['id']=$id; 
			if(M('user')->save($row)){
				$this->assign('jumpUrl', U('Index/index'));
				$this->success("恭喜您，激活成功！");
			}else{
				$this->assign('jumpUrl', U('Index/index'));
				$this->success("已经激活！");
			}
		}else{ 
			$msg = 'error.';     
		} 
		echo $msg;
	}
	
	function login(){
		
		$this->display();	
		
	}
	
	function login_do(){
		$username=$_POST["user_namea"];
		$password=$_POST["user_passa"];
		if($username=='' || $password==''){
			$this->error("不能为空");	
		}
		$pass=md5($_POST["user_passa"]);
		if(M("user")->where("user_name='$username'")->select()){
			if(!M("user")->where("user_name='$username' and user_pass='$pass'")->select()){
				$this->error("密码错误");	
			}else{
				$aa=M("user")->where("user_name='$username' and user_pass='$pass'")->select();
				$user=$aa[0];
				$_SESSION["user_id"]=$user["id"];
				$_SESSION["user_name"]=$user["user_name"];
				$_SESSION["nicheng"]=$user["nicheng"];
				//获取积分，加载称号等级
				$user_yuan=M('user')->find($user["id"]);
				$user_xian_chenghao['id']=$user_yuan['id'];
				if($user_yuan['jifen']<50){
					$user_xian_chenghao['chenghao']="0";	
				}else{
					if($user_yuan['jifen']<100){
						$user_xian_chenghao['chenghao']="1";
					}else{
						if($user_yuan['jifen']<500){
							$user_xian_chenghao['chenghao']="2";	
						}else{
							if($user_yuan['jifen']<1000){
								$user_xian_chenghao['chenghao']="3";
							}else{
								if($user_yuan['jifen']<5000){
									$user_xian_chenghao['chenghao']="4";		
								}else{
									if($user_yuan['jifen']<10000){
										$user_xian_chenghao['chenghao']="5";
									}else{
										if($user_yuan['jifen']<50000){
											$user_xian_chenghao['chenghao']="6";
										}else{
											if($user_yuan['jifen']<100000){
												$user_xian_chenghao['chenghao']="7";
											}else{
												if($user_yuan['jifen']<500000){
													$user_xian_chenghao['chenghao']="8";
												}else{
													if($user_yuan['jifen']<1000000){
														$user_xian_chenghao['chenghao']="9";
													}else{
														if($user_yuan['jifen']<2000000){
															$user_xian_chenghao['chenghao']="10";
														}else{
															if($user_yuan['jifen']<5000000){
																$user_xian_chenghao['chenghao']="11";
															}else{
																if($user_yuan['jifen']<8000000){
																	$user_xian_chenghao['chenghao']="12";
																}else{
																	if($user_yuan['jifen']<10000000){
																		$user_xian_chenghao['chenghao']="13";
																	}else{
																		$user_xian_chenghao['chenghao']="14";
																	}	
																}	
															}	
														}	
													}	
												}	
											}	
										}	
									}	
								}
							}
						}
					}	
				}
				//获取达人票数，加载达人等级
				if($user_yuan['darenpiaoshu']<5000){
					$user_xian_chenghao['darenjishu']="0";	
				}else{
					if($user_yuan['darenpiaoshu']<10000){
						$user_xian_chenghao['darenjishu']="1";
					}else{
						if($user_yuan['darenpiaoshu']<50000){
							$user_xian_chenghao['darenjishu']="2";	
						}else{
							if($user_yuan['darenpiaoshu']<100000){
								$user_xian_chenghao['darenjishu']="3";
							}else{
								if($user_yuan['darenpiaoshu']<500000){
									$user_xian_chenghao['darenjishu']="4";		
								}else{
									if($user_yuan['darenpiaoshu']<1000000){
										$user_xian_chenghao['darenjishu']="5";
									}else{
										if($user_yuan['darenpiaoshu']<2000000){
											$user_xian_chenghao['darenjishu']="6";
										}else{
											if($user_yuan['darenpiaoshu']<5000000){
												$user_xian_chenghao['darenjishu']="7";
											}else{
												if($user_yuan['darenpiaoshu']<8000000){
													$user_xian_chenghao['darenjishu']="8";
												}else{
													if($user_yuan['darenpiaoshu']<10000000){
														$user_xian_chenghao['darenjishu']="9";
													}else{
														$user_xian_chenghao['darenjishu']="10";
													}	
												}	
											}	
										}	
									}	
								}
							}
						}
					}	
				}

				//7、8、9、10称号升级时加小喇叭
				if($user_xian_chenghao['chenghao']==$user_yuan['chenghao']){
					
				}else{
					if($user_xian_chenghao['chenghao']=="7"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+1;	
					}
					if($user_xian_chenghao['chenghao']=="8"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+2;	
					}
					if($user_xian_chenghao['chenghao']=="9"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+3;	
					}
					if($user_xian_chenghao['chenghao']=="10"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+6;	
					}
					
					M('user')->save($user_xian_chenghao);
				}
				//达人升级时加载
				if($user_xian_chenghao['darenjishu']==$user_yuan['darenjishu']){
					
				}else{
					//1、2等级达人升级时加置顶次数
					if($user_xian_chenghao['darenjishu']=="1"){
						$user_xian_chenghao['zhidingcishu']=$user_yuan['zhidingcishu']+1;	
					}
					if($user_xian_chenghao['darenjishu']=="2"){
						$user_xian_chenghao['zhidingcishu']=$user_yuan['zhidingcishu']+1;	
					}
					//3、4、5、6等级达人升级时加小喇叭
					if($user_xian_chenghao['darenjishu']=="3"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+1;	
					}
					if($user_xian_chenghao['darenjishu']=="4"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+2;	
					}
					if($user_xian_chenghao['darenjishu']=="5"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+3;	
					}
					if($user_xian_chenghao['darenjishu']=="6"){
						$user_xian_chenghao['xiaolabashu']=$user_yuan['xiaolabashu']+6;	
					}
					
					M('user')->save($user_xian_chenghao);
				}

				//获取上一次初始化时间，重新初始化相关的权限和操作次数限制
				$useryuanxinxi=M('user')->find($user["id"]);
				$xianchushihuatime=strtotime(date("Ymd",time()));
				//获取上一次称号初始化时间
				$yuanrichushihuatime=$useryuanxinxi['ricshtime'];
				if($xianchushihuatime==$yuanrichushihuatime){
					
				}else{
					$userjiazai['ricshtime']=$xianchushihuatime;
					$userjiazai['id']=$user["id"];
					$userjiazai['yueducishu']="10";
					$userjiazai['zancishu']="10";
					$userjiazai['caicishu']="10";
					$userjiazai['zhuanfacishu']="10";
					$userjiazai['pingluncishu']="10";
					
					if($useryuanxinxi['chenghao']=="14"){
						$userjiazai['tuijiancishu']="6";
						$userjiazai['jiajingcishu']="6";
						$userjiazai['xuantucishu']="1";
						$userjiazai['wenziliancishu']="1";
						$userjiazai['jiaodiantucishu']="1";
						$userjiazai['xiaolabashu']="1";
						$userjiazai['shoudarentuijiancishu']="1";
						$userjiazai['gedarentuijiancishu']="1";
							
					}else{
						if($useryuanxinxi['chenghao']=="13"){
							$userjiazai['tuijiancishu']="6";
							$userjiazai['jiajingcishu']="6";
							$userjiazai['jiaodiantucishu']="1";
							$userjiazai['shoudarentuijiancishu']="1";
							$userjiazai['xiaolabashu']="1";
								
						}else{
							if($useryuanxinxi['chenghao']=="12"){
								$userjiazai['tuijiancishu']="6";
								$userjiazai['jiajingcishu']="6";
								$userjiazai['wenziliancishu']="1";
								$userjiazai['gedarentuijiancishu']="1";
								$userjiazai['xiaolabashu']="1";
									
							}else{
								if($useryuanxinxi['chenghao']=="11"){
									$userjiazai['tuijiancishu']="6";
									$userjiazai['jiajingcishu']="6";
									$userjiazai['xuantucishu']="1";
									$userjiazai['xiaolabashu']="1";
									
								}else{
									if($useryuanxinxi['chenghao']=="10"){
										$userjiazai['tuijiancishu']="5";
										$userjiazai['jiajingcishu']="5";
									}else{
										if($useryuanxinxi['chenghao']=="9"){
											$userjiazai['tuijiancishu']="4";
											$userjiazai['jiajingcishu']="4";
										}else{
											if($useryuanxinxi['chenghao']=="8"){
												$userjiazai['tuijiancishu']="3";
												$userjiazai['jiajingcishu']="3";
											}else{
												if($useryuanxinxi['chenghao']=="7"){
													$userjiazai['tuijiancishu']="2";
													$userjiazai['jiajingcishu']="2";
												}else{
													if($useryuanxinxi['chenghao']=="6"){
														$userjiazai['tuijiancishu']="1";
														$userjiazai['jiajingcishu']="1";
													}else{
	
													}	
												}	
											}	
										}	
									}
								}
							}
						}	
					}
	
					M('user')->save($userjiazai);
				}
				//获取上一次达人初始化时间
				$yuandarenrichushihuatime=$useryuanxinxi['darenricshtime'];
				if($xiandarenchushihuatime==$yuandarenrichushihuatime){
					
				}else{
					$userjiazaidarendaren['darenricshtime']=$xiandarenchushihuatime;
					$userjiazaidaren['id']=$user["id"];
					
					if($useryuanxinxi['darenjishu']=="10"){
						$userjiazaidaren['darentoupiaocishu']="12";
						$userjiazaidaren['xiaolabashu']="1";
						$userjiazaidaren['shoudarentuijiancishu']="2";
							
					}else{
						if($useryuanxinxi['darenjishu']=="9"){
							$userjiazaidaren['darentoupiaocishu']="11";
							$userjiazaidaren['shoudarentuijiancishu']="1";
							$userjiazaidaren['xiaolabashu']="1";
								
						}else{
							if($useryuanxinxi['darenjishu']=="8"){
								$userjiazaidaren['darentoupiaocishu']="10";
								$userjiazaidaren['gedarentuijiancishu']="1";
								$userjiazaidaren['xiaolabashu']="1";
									
							}else{
								if($useryuanxinxi['darenjishu']=="7"){
									$userjiazaidaren['darentoupiaocishu']="9";
									$userjiazaidaren['xiaolabashu']="1";
									
								}else{
									if($useryuanxinxi['darenjishu']=="6"){
										$userjiazaidaren['darentoupiaocishu']="8";
									}else{
										if($useryuanxinxi['darenjishu']=="5"){
											$userjiazaidaren['darentoupiaocishu']="7";
										}else{
											if($useryuanxinxi['darenjishu']=="4"){
												$userjiazaidaren['darentoupiaocishu']="6";
											}else{
												if($useryuanxinxi['darenjishu']=="3"){
													$userjiazaidaren['darentoupiaocishu']="5";
												}else{
													if($useryuanxinxi['darenjishu']=="2"){
														$userjiazaidaren['darentoupiaocishu']="4";
													}else{
														if($useryuanxinxi['darenjishu']=="1"){
															$userjiazaidaren['darentoupiaocishu']="3";
														}else{
															$userjiazaidaren['darentoupiaocishu']="2";
														}
													}	
												}	
											}	
										}	
									}
								}
							}
						}	
					}
	
					M('user')->save($userjiazaidaren);
				}
				
				$this->assign('jumpUrl', U('Index/grwbsy'));
				$this->success("登录成功");
			}
		}else{
			$this->error("此邮箱未注册");
		}	
	}
	
	function logout(){
		unset($_SESSION['user_id']);
		unset($_SESSION['user_name']);
		$this->assign('jumpUrl', U('Index/index'));
		$this->success("退出成功");
	}
   function yhzx(){
 	    $user_id=$_SESSION["user_id"];
        if($user_id==''){
		   $this->assign('jumpUrl', U('User/login'));
		   $this->error("请登录");  
		}

		$this->display('yhzx');  
		  
   }
      function yhzx_do(){
		
			 $user_id=$_SESSION["user_id"];
			if($user_id==''){
			   $this->assign('jumpUrl', U('User/login'));
			   $this->error("请登录");  
			}
			$user=M('user')->where("id=$user_id")->find();
			$this->assign($user);
			
			$user_passy=md5($_POST['user_passy']);
			if($user_passy!=$user['user_pass']){
			     $this->error("原密码错误"); exit;
			}
			
			$yanzheng=strtolower($_POST["vcode"]);
			$aa=strtolower($_SESSION['vcode']);
			if($aa!=$yanzheng){
				$this->error("验证码错误");	exit;
			}
			if($_POST["user_pass"]==''){
				$this->error("信息不完整");	exit;
			}

			
			$_POST['id']=$_SESSION["user_id"];
			$_POST['user_pass']=md5($_POST['user_pass']);

		  if( M('user')->save($_POST) ){
				$this->assign('jumpUrl', U('User/yhzx'));
				$this->success('修改成功！');
		  }else{
				$this->error('修改失败！');
		  }

	 }
	 
	//找回密码
	
	 function zhmm(){

		$this->display();  
		  
   }
      function zhmm_do(){
			$user_name=$_POST['user_name'];
			$user=M('user')->where(" user_name='$user_name'")->find();
			if($user['id']==""){
				$this->error("此邮箱尚未注册");	exit;
			}			
			$yanzheng=strtolower($_POST["vcode"]);
			$aa=strtolower($_SESSION['vcode']);
			if($aa!=$yanzheng){
				$this->error("验证码错误");	exit;
			}
			
			$ll=rand(100000, 999999).$user['id'];
			$user['user_pass']=md5($ll);
			M('user')->save($user);
			sendemail($user['user_name'], 'lexiyou', $user['user_name'], "您好，".$user['nicheng']."  您的密码已修改为:\r\n".$ll."\r\n"."此邮件是验证邮件，不必回复。","会员密码找回");
			
			$this->assign('jumpUrl', U('Index/index'));
			$this->success("恭喜您，提交成功！请登录到您的邮箱查看新密码！");

	 }
	  
	  
	   
	  
	 
	
} 